package com.example.hjt_java_web_homework2.infrastructure.persistence;

import com.example.hjt_java_web_homework2.domain.model.AirConditioner;
import com.example.hjt_java_web_homework2.domain.repository.AirConditionerRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * @author Li Chun Hei
 * @date 31/10/2025
 * @description
 */

@Repository
public interface AirConditionerRepositoryImpl extends JpaRepository<AirConditioner, Long>, AirConditionerRepository {

    @Override
    default AirConditioner save(AirConditioner airConditioner) {
        return saveAndFlush(airConditioner);
    }

//    @Override
//    default Optional<AirConditioner> findById(Long id) {
//        return findById(id);
//    }

    @Query("SELECT ac FROM AirConditioner ac WHERE ac.deviceId = :deviceId")
    Optional<AirConditioner> findByDeviceId(@Param("deviceId") String deviceId);

//    @Override
//    default List<AirConditioner> findAll() {
//        return findAll();
//    }

//    @Override
//    default void deleteById(Long id) {
//        deleteById(id);
//    }

    @Query("SELECT COUNT(ac) > 0 FROM AirConditioner ac WHERE ac.deviceId = :deviceId")
    boolean existsByDeviceId(@Param("deviceId") String deviceId);
}
